<template>
  <van-button class="follow-btn" type="info" color="#3296fa" round size="small" icon="plus" v-if='!Followed'
    @click='onFollowed' :loading='isfollowedloading'>关注
  </van-button>
  <van-button class="follow-btn" round size="small" v-else @click='onFollowed' :loading='isfollowedloading'>已关注
  </van-button>
</template>

<script>
// 关注请求 取关请求
import { addUserFailure, deleteUserFailure } from '@/api/user'
export default {
  data () {
    return {
      // 点击关注加载状态
      isfollowedloading: false
    }
  },
  props: {
    // 父传子关注状态
    Followed: {
      type: Boolean,
      required: true
    },
    // 父传子关注ID
    autId: {
      type: [Object, Number],
      required: true
    }
  },
  // 修改v-model解析规则
  model: {
    // 属性解析
    prop: 'Followed',
    // 事件解析
    event: 'updateFollowed'
  },
  methods: {
    // 点击关注按钮
    async onFollowed () {
      this.isfollowedloading = true
      try {
        if (this.Followed) {
          await deleteUserFailure(this.autId)
          this.$toast('取关成功')
        } else {
          await addUserFailure(this.autId)
          this.$toast('关注成功')
        }
        this.$emit('updateFollowed', !this.Followed)
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return this.$toast('不能关注自己')
        }
        this.$toast('操作失败')
      }
      this.isfollowedloading = false
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
